package com.zx.ishopping.controller.admin;

import com.zx.ishopping.DTO.ProductDto;
import com.zx.ishopping.DTO.ProductPageDTO;
import com.zx.ishopping.entity.Products;
import com.zx.ishopping.results.PageResult;
import com.zx.ishopping.results.Result;
import com.zx.ishopping.services.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController("adminProductController")
@RequestMapping("/admin/product")
public class ProductController {
    
    @Autowired
    private ProductService productService;
    
    
    /**
     * 根据分类id获取商品数量
     * @param categoryId
     * @return
     */
    @GetMapping("/getNumByCategoryId/{categoryId}")
    public Result<Long> getProductNumByCategoryId(@PathVariable Long categoryId){
       Long productNum = productService.getProductNumByCategoryId(categoryId);
       return Result.success(productNum);
    }


    /**
     * 分页查询分类商品
     * @param productPageDTO
     * @return
     */
    @GetMapping("/pageQuery")
    public  Result<PageResult> pageQuery( ProductPageDTO productPageDTO){
       PageResult pageResult = productService.pageQuery(productPageDTO);
       return Result.success(pageResult);
    }


    /**
     * 修改商品
     * @param products
     * @return
     */
    @PutMapping("/update")
    public Result updateProduct(@RequestBody Products products){
        log.info("修改商品信息：{}",products);
       productService.updateProduct(products);
        return Result.success();
    }

    /**
     * 批量删除商品
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteProducts")
    public Result deleteProduct(@RequestBody List<Long> ids ){
        log.info("删除商品：{}",ids);
        productService.deleteProduct(ids);
        return Result.success();
    }
    
    /**
     * 添加商品
     * @param productDto
     * @return
     */
    @PostMapping("/addProduct")
    public Result addProduct(@RequestBody ProductDto productDto){
        log.info("添加商品：{}",productDto);
        productService.addProduct(productDto);
        return Result.success();
        
    }
    
}
